<?php
/**
 * GioCMS
 * 
 * LICENSE
 *
 * This source file is subject to the GNU GENERAL PUBLIC LICENSE Version 2 
 * that is bundled with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://www.gnu.org/licenses/gpl-2.0.txt
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@ninhgio.com so we can send you a copy immediately.
 * 
 * @copyright	Copyright (c) 2010-2011 GioCMS (http://cms.ninhgio.com)
 * @license		http://www.gnu.org/licenses/gpl-2.0.txt GNU GENERAL PUBLIC LICENSE Version 2
 * @author		NinhGio - ninhgio@gmail.com
 * @since		1.0
 */

class Modules_Core_Models_Mysql_Module extends Modules_Core_Models_Module
{
	public function getById($id)
	{
		$sql = 'SELECT * FROM ' . $this->_conn->_tablePrefix . 'core_module 
							WHERE module_id = "' . $this->_conn->escape($id) . '" LIMIT 1';
		$rs = $this->_conn->query($sql);
		$return = (mysql_num_rows($rs) == 0) ? null : $this->_conn->fetchAll($rs);
		return $return;
	}
	
	public function getModulesInstalled()
	{
		$sql = 'SELECT * FROM ' . $this->_conn->_tablePrefix . 'core_module ORDER BY module_id ASC';
		$rs = $this->_conn->query($sql);
		$modules = array();
		while ($row = $this->_conn->fetchAll($rs)) {
			$modules[] = $row;
		}
		/**
		 * Free result 
		 */
		$this->_conn->freeResult($rs);
		return $modules;
	}
	
	public function add($module)
	{
		$this->_conn->insert($module, $this->_conn->_tablePrefix . 'core_module');
	}
	
	public function delete($moduleId)
	{
		$sql = 'DELETE FROM ' . $this->_conn->_tablePrefix . 'core_module 
							WHERE module_id = "' . $this->_conn->escape($moduleId) . '"';
		$rs = $this->_conn->query($sql);
		$affectRow = $this->_conn->affectedRows();
		$this->_conn->freeResult($rs);
		return $affectRow;
	}
	
	public function install($module)
	{
		$file = ROOT_DIR . DS . 'modules' . DS . $module . DS . 'configs' . DS . 'about.xml';
		if (!file_exists($file)) {
			return null;
		}
		
		$xml = simplexml_load_file($file);
		$moduleArr = array(
			'name' 		  => strtolower($xml->name),
			'description' => (string)$xml->description,
			//'thumbnail'   => $xml->thumbnail,
			//'author' 	  => $xml->author,
			//'email' 	  => $xml->email,
			//'version' 	  => $xml->version,
			//'license' 	  => $xml->license,
		);		
		
		/**
		 * Execute install scripts
		 */
		$xpath = $xml->xpath('install/query');
		if (is_array($xpath) && count($xpath) > 0) {
			foreach ($xpath as $query) {
				try {
					$query = str_replace('###table_prefix###', $this->_conn->_tablePrefix, (string)$query);
					$this->_conn->query($query);
				} catch (Exception $ex) {
					break;
				}
			}
		}
		
		return $moduleArr;	
	}
	
	public function uninstall($module)
	{
		$ret  = $this->delete($module);
		
		$file = ROOT_DIR . DS . 'modules' . DS . $module . DS . 'configs' . DS . 'about.xml';
		if (!file_exists($file)) {
			return 0;
		}
		$xml = simplexml_load_file($file);
		
		/**
		 * Execute uninstall scripts
		 */
		$xpath = $xml->xpath('uninstall/query');
		if (is_array($xpath) && count($xpath) > 0) {
			foreach ($xpath as $query) {
				try {
					$query = str_replace('###table_prefix###', $this->_conn->_tablePrefix, (string)$query);
					$this->_conn->query($query);
				} catch (Exception $ex) {
					break;
				}
			}
		}
		
		return $ret;
	}
}